<template>
  <el-dialog
    :title="!dataForm.gsmGroupId ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
    <el-form-item label="拼团商品" prop="psmId">
      <el-input v-model="dataForm.psmId" placeholder="拼团商品"></el-input>
    </el-form-item>
    <el-form-item label="活动封面" prop="gsmGroupImg">
      <el-input v-model="dataForm.gsmGroupImg" placeholder="活动封面"></el-input>
    </el-form-item>
    <el-form-item label="活动名称" prop="gsmGroupName">
      <el-input v-model="dataForm.gsmGroupName" placeholder="活动名称"></el-input>
    </el-form-item>
    <el-form-item label="生效时间" prop="gsmGroupStartTime">
      <el-input v-model="dataForm.gsmGroupStartTime" placeholder="生效时间"></el-input>
    </el-form-item>
    <el-form-item label="过期时间" prop="gsmGroupEndTime">
      <el-input v-model="dataForm.gsmGroupEndTime" placeholder="过期时间"></el-input>
    </el-form-item>
    <el-form-item label="拼团库存" prop="gsmGroupStock">
      <el-input v-model="dataForm.gsmGroupStock" placeholder="拼团库存"></el-input>
    </el-form-item>
    <el-form-item label="拼团价格" prop="gsmGroupPrice">
      <el-input v-model="dataForm.gsmGroupPrice" placeholder="拼团价格"></el-input>
    </el-form-item>
    <el-form-item label="成团人数" prop="gsmGroupCount">
      <el-input v-model="dataForm.gsmGroupCount" placeholder="成团人数"></el-input>
    </el-form-item>
    <el-form-item label="成团时效" prop="gsmGroupDuration">
      <el-input v-model="dataForm.gsmGroupDuration" placeholder="成团时效"></el-input>
    </el-form-item>
    <el-form-item label="限购数量" prop="gsmGroupLimitCount">
      <el-input v-model="dataForm.gsmGroupLimitCount" placeholder="限购数量"></el-input>
    </el-form-item>
    <el-form-item label="运费" prop="gsmGroupFreight">
      <el-input v-model="dataForm.gsmGroupFreight" placeholder="运费"></el-input>
    </el-form-item>
    <el-form-item label="奖金" prop="gsmGroupBonus">
      <el-input v-model="dataForm.gsmGroupBonus" placeholder="奖金"></el-input>
    </el-form-item>
    <el-form-item label="分享描述" prop="gsmGroupShareDescription">
      <el-input v-model="dataForm.gsmGroupShareDescription" placeholder="分享描述"></el-input>
    </el-form-item>
    <el-form-item label="拼团状态" prop="gsmStatus">
      <el-input v-model="dataForm.gsmStatus" placeholder="拼团状态"></el-input>
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        dataForm: {
          gsmGroupId: 0,
          psmId: '',
          gsmGroupImg: '',
          gsmGroupName: '',
          gsmGroupStartTime: '',
          gsmGroupEndTime: '',
          gsmGroupStock: '',
          gsmGroupPrice: '',
          gsmGroupCount: '',
          gsmGroupDuration: '',
          gsmGroupLimitCount: '',
          gsmGroupFreight: '',
          gsmGroupBonus: '',
          gsmGroupShareDescription: '',
          gsmStatus: ''
        },
        dataRule: {
          psmId: [
            { required: true, message: '拼团商品不能为空', trigger: 'blur' }
          ],
          gsmGroupImg: [
            { required: true, message: '活动封面不能为空', trigger: 'blur' }
          ],
          gsmGroupName: [
            { required: true, message: '活动名称不能为空', trigger: 'blur' }
          ],
          gsmGroupStartTime: [
            { required: true, message: '生效时间不能为空', trigger: 'blur' }
          ],
          gsmGroupEndTime: [
            { required: true, message: '过期时间不能为空', trigger: 'blur' }
          ],
          gsmGroupStock: [
            { required: true, message: '拼团库存不能为空', trigger: 'blur' }
          ],
          gsmGroupPrice: [
            { required: true, message: '拼团价格不能为空', trigger: 'blur' }
          ],
          gsmGroupCount: [
            { required: true, message: '成团人数不能为空', trigger: 'blur' }
          ],
          gsmGroupDuration: [
            { required: true, message: '成团时效不能为空', trigger: 'blur' }
          ],
          gsmGroupLimitCount: [
            { required: true, message: '限购数量不能为空', trigger: 'blur' }
          ],
          gsmGroupFreight: [
            { required: true, message: '运费不能为空', trigger: 'blur' }
          ],
          gsmGroupBonus: [
            { required: true, message: '奖金不能为空', trigger: 'blur' }
          ],
          gsmGroupShareDescription: [
            { required: true, message: '分享描述不能为空', trigger: 'blur' }
          ],
          gsmStatus: [
            { required: true, message: '拼团状态不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.gsmGroupId = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.gsmGroupId) {
            this.$http({
              url: this.$http.adornUrl(`/group/group/info/${this.dataForm.gsmGroupId}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.psmId = data.group.psmId
                this.dataForm.gsmGroupImg = data.group.gsmGroupImg
                this.dataForm.gsmGroupName = data.group.gsmGroupName
                this.dataForm.gsmGroupStartTime = data.group.gsmGroupStartTime
                this.dataForm.gsmGroupEndTime = data.group.gsmGroupEndTime
                this.dataForm.gsmGroupStock = data.group.gsmGroupStock
                this.dataForm.gsmGroupPrice = data.group.gsmGroupPrice
                this.dataForm.gsmGroupCount = data.group.gsmGroupCount
                this.dataForm.gsmGroupDuration = data.group.gsmGroupDuration
                this.dataForm.gsmGroupLimitCount = data.group.gsmGroupLimitCount
                this.dataForm.gsmGroupFreight = data.group.gsmGroupFreight
                this.dataForm.gsmGroupBonus = data.group.gsmGroupBonus
                this.dataForm.gsmGroupShareDescription = data.group.gsmGroupShareDescription
                this.dataForm.gsmStatus = data.group.gsmStatus
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/group/group/${!this.dataForm.gsmGroupId ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'gsmGroupId': this.dataForm.gsmGroupId || undefined,
                'psmId': this.dataForm.psmId,
                'gsmGroupImg': this.dataForm.gsmGroupImg,
                'gsmGroupName': this.dataForm.gsmGroupName,
                'gsmGroupStartTime': this.dataForm.gsmGroupStartTime,
                'gsmGroupEndTime': this.dataForm.gsmGroupEndTime,
                'gsmGroupStock': this.dataForm.gsmGroupStock,
                'gsmGroupPrice': this.dataForm.gsmGroupPrice,
                'gsmGroupCount': this.dataForm.gsmGroupCount,
                'gsmGroupDuration': this.dataForm.gsmGroupDuration,
                'gsmGroupLimitCount': this.dataForm.gsmGroupLimitCount,
                'gsmGroupFreight': this.dataForm.gsmGroupFreight,
                'gsmGroupBonus': this.dataForm.gsmGroupBonus,
                'gsmGroupShareDescription': this.dataForm.gsmGroupShareDescription,
                'gsmStatus': this.dataForm.gsmStatus
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
